package com.example.zuoye.controller;


import com.example.zuoye.entity.Book;
import com.example.zuoye.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    User user = new User();

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/home")
    public String index(HttpSession session,
                        Model model){
//        List<Book> books = bookService.selectBookInfo();
        session.setAttribute("book_name","说不尽的废名");
        session.setAttribute("book_press","商务印书馆");
        session.setAttribute("book_author","陈建军");
        session.setAttribute("book_date","2021-11-15");
        session.setAttribute("book_number","100");
        session.setAttribute("book_image","https://booklibimg.kfzimg.com/data/book_lib_img_v2/user/0/80ee/80eeceb9079af42da2cec2dc498ec471_0_0_0_0_water.jpg");

        List booksInfo = new ArrayList();
        List bookInfo = new ArrayList();
        for(int i = 0;i < 5;i++){
            List bookTarget = new ArrayList();
            bookTarget.add(session.getAttribute("book_name"));
            bookTarget.add(session.getAttribute("book_press"));
            bookTarget.add(session.getAttribute("book_author"));
            bookTarget.add(session.getAttribute("book_date"));
            bookTarget.add(session.getAttribute("book_number"));
            bookTarget.add(session.getAttribute("book_image"));
            bookInfo.add(bookTarget);
        }
        booksInfo.add(bookInfo);
        model.addAttribute("booksInfo",bookInfo);
        return "home";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @Autowired
    HttpServletRequest request; //通过注解获取一个request

    @RequestMapping("/ulogin")
    public String login(HttpSession session,
                        Model model) throws Exception {


        String userName = (String) request.getSession().getAttribute("re_username");
        String passWord = (String) request.getSession().getAttribute("re_password");
        System.out.println("执行了ulogin方法");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println(username);
//        Object usernameSE = session.getAttribute(username);
//        System.out.println(usernameSE);
        if (!username.equals("root")){
            if (username.equals(userName)){
                if (password.equals(passWord)){
                    session.setAttribute("username",username);
                    session.setAttribute("password",password);
                    return "redirect:/user/home";
                }else{
                    model.addAttribute("tip_registered","密码不正确");
                    return "login";
                }
            }else{
                System.out.println("该用户名不存在");
                model.addAttribute("tip_registered","该用户名不存在");
                return "login";
            }
        }else{
            // 查询用户信息
//            user = userService.checkByUP(username, password);
//            String passwordSE = (String) session.getAttribute(password);
            System.out.println(password);
            if (password.equals("123456")){
//                session.setAttribute("user_login",user);
                System.out.println("登录");
                session.setAttribute("username",username);
                session.setAttribute("password",password);
                return "redirect:/user/home";
            }else{
//                model.addAttribute("tip_registered","密码不正确");
                return "login";
            }
        }
    }

    @RequestMapping("/uregister")
    public String register(HttpServletRequest request,
                           Model model) throws Exception {

        HttpSession session = request.getSession();

        System.out.println("执行了uregister方法");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        String phone = request.getParameter("phone");

        if (username == null || password == null || password2 == null || phone == null) {
            model.addAttribute("tip_registered","注册信息不能为空");
            return "login";
        }

        // 对两次输入的密码进行校验
        if (!password.equals(password2)){
            model.addAttribute("tip_registered","两次输入的密码不一致");
            return "login";
        }
        // 数据库中查询username是否存在,不存在则插入,存在则提示:该用户名已经存在
//        String usernameDB = userService.findUserNameByUserName(username);
        System.out.println(username);
        Object usernameSE = session.getAttribute(username);

        if (usernameSE == null){
            session.setAttribute("re_username",username);
            session.setAttribute("re_password",password);
            session.setAttribute("re_phone",phone);
//            userService.insertUser(user);
            return "redirect:/user/home";
        }else{
            model.addAttribute("tip_registered","该用户名已注册");
            return "login";
        }



    }
}
